<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <el-form
          v-show="showSearch"
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          label-width="68px"
        >
          <el-form-item label="科室名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入科室名称"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索
            </el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="handleDelete"
            >删除
            </el-button>
          </el-col>

          <!-- <right-toolbar
            :show-search.sync="showSearch"
            :columns="columns"
            @queryTable="getList"
          /> -->
        </el-row>

        <el-table :data="departmentlist" style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"/>
          <el-table-column prop="departmentName" label="科室名称" align="center"/>
          <el-table-column  v-if="false" prop="id" label="id" align="center" hidden/>
          <el-table-column prop="briefIntroduction" label="科室简介" align="center"/>
          <el-table-column prop="location" label="科室地址" align="center"/>
          <el-table-column prop="phone" label="联系电话" align="center">
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" align="center"/>

          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template v-if="scope.row.id !== 1" slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <el-pagination
      :page-size.sync="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      :current-page.sync="queryParams.pageNum"
      @current-change="getList"
      @size-change="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="科室名称" prop="departmentName">
              <el-input
                v-model="form.departmentName"
                placeholder="请输入科室名称"
                maxlength="30"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="科室地址" prop="location">
              <el-input
                v-model="form.location"
                placeholder="请输入科室地址"
                maxlength="30"
              />
            </el-form-item>
          </el-col>

          <el-col :span="12">
              <el-form-item label="联系电话" prop="phone">
                <el-input
                  v-model="form.phone"
                  placeholder="请输入联系电话"
                  maxlength="11"
                />
              </el-form-item>
          </el-col>
        </el-row>

            <el-form-item label="科室简介" prop="briefIntroduction">
              <el-input
                v-model="form.briefIntroduction"
                placeholder="请输入科室简介"
                maxlength="30"
                type="textarea"
              />
            </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {allDepartmentList, deleteDepartment,addDepartment,updateDepartment } from "@/api/department/department";

export default {
  name: 'User',
  data() {
    let self = this;
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
      },
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单校验
      rules: {
        departmentName: [
          {required: true, trigger: 'blur' ,message: '科室名称不能为空'}
        ],
        briefIntroduction: [
          {required: true, message: '科室简介不能为空', trigger: 'blur'},
        ],
        location: [
          {required: true, message: '科室位置不能为空', trigger: 'blur'},
        ],
        phone: [
          {
            required: true,
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: '请输入正确的手机号码',
            trigger: 'blur'
          }
        ]
      },

      // 显示搜索条件
      showSearch: true,
      // 遮罩层
      loading: true,
      // 用户表格数据
      departmentlist: null,
      // 总条数
      total: 0,
      // 选中数组
      ids: [],
      // 表单参数
      form: {}
    }
  },
  watch: {},
  created() {
    this.getList()
  },
  methods: {
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },

    getList() {
      this.loading = true
      allDepartmentList(this.queryParams).then((response) => {
        this.departmentlist = response.list
        this.total = response.total
        this.loading = false
      })
    },

    // 表单重置
    reset() {
      this.form = {
        userName: undefined,
        score: undefined,
        scoreDetail: undefined,
        type: undefined,
        userId: undefined,
        id:undefined
      }
      this.resetForm('form')
    },
    /** 添加积分 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加部门'

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否删除当前选中数据项？')
        .then(function () {
          return deleteDepartment(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
    },
    handleUpdate(row) {
      this.reset()
      this.form = row
      this.open = true
      this.title = '修改部门'
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateDepartment(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          }else{
            addDepartment(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }

        }
      })
    }
  }
}
</script>
